<?php
namespace app\backend\controller;

class RecycleShop extends Backend {
	
	public function index() {
		
		$view = \think\View::instance();
		$request = \think\Request::instance();
		
		$where = array();
		$condition = array();
		$condition['shop_title'] = $request->get('shop_title', '');
		if(0 < mb_strlen($condition['shop_title'], 'utf8')){
			$where['subscriber_group.title'] = array('like', '%%'.$condition['shop_title'].'%%');
		};
		$condition['shop_account'] = $request->get('shop_account', '');
		if(0 < mb_strlen($condition['shop_account'], 'utf8')){
			$where['subscriber_group.account'] = array('like', '%%'.$condition['shop_account'].'%%');
		};
		$view->assign('condition', $condition);
		
		$subscriber_number = \app\common\model\SubscriberModel::field('COUNT(`subscriber`.`id`)')->where('`subscriber`.`group_id` = `subscriber_group`.`id`')->buildsql();
		$order_number = \app\common\model\RecycleOrderNumberModel::field('COUNT(`recycle_order_number`.`id`)')->where('`recycle_order_number`.`group_id` = `subscriber_group`.`id`')->buildsql();
		$order_count = \app\common\model\RecycleOrderNumberModel::field('SUM(`recycle_order_number`.`number`)')->where('`recycle_order_number`.`group_id` = `subscriber_group`.`id`')->buildsql();
		$field = array(
			'`subscriber_group`.*',
			$subscriber_number . ' AS `subscriber_number`',
			$order_number . 'AS `order_number`',
			'IFNULL(' . $order_count . ', 0) AS `order_count`',
		);
		$banner_select = \app\common\model\SubscriberGroupModel::field($field)
			->where('subscriber_group.is_del',0)->where($where)
			->order('subscriber_group.id DESC')->paginate(10, false, array('query'=>$condition));
		$view->assign('page', $banner_select->render());
		
		$data_list = array();
		foreach($banner_select as $StaffModel){
			$data = $StaffModel->toArray();
			$data_list[] = $data;
		};
		$view->assign('data_list', $data_list);
		
		unset($where, $condition, $field, $banner_select, $StaffModel);
		return $view->fetch();
		
	}
	
	public function del() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$SubscriberGroupModel = \app\common\model\SubscriberGroupModel::where('id', $parameter['id'])->find();
		if(null === $SubscriberGroupModel){
			return $this->disposeJson(1, '对应的店铺不存在，请联系管理员');
		};
		
		\app\common\model\Model::startTrans();
		try{
			
			// 解绑用户
			$result = \app\common\model\SubscriberModel::where('group_id', $SubscriberGroupModel->id)->update(['group_id' => 0]);
			if(false === $result){
				throw new \Exception('解绑用户失败');
			};
			
			// 修改店铺状态
			$SubscriberGroupModel->is_del = 1;
			$result = $SubscriberGroupModel->save();
			if(false == $result){
				throw new \Exception('修改店铺状态失败');
			};
			
			\app\common\model\Model::commit();
			
		}catch(\Exception $error){
			
			\app\common\model\Model::rollback();
			$message = is_string($error->getMessage()) ? $error->getMessage() : json_encode($error->getMessage());
			return $this->disposeJson(1, $message);
			
		};
		
		return $this->disposeJson(0, '操作成功，即将跳转');
	}
	
	public function member() {
		
		$view = \think\View::instance();
		$request = \think\Request::instance();
		
		$group_id = $request->get('group_id');
		if(!is_string($group_id) || (1 !== preg_match('/^[0-9]{1,10}$/', $group_id)) || !settype($group_id, 'integer')){
			return $this->disposeHtml('关键参数不存在', $view);
		};
		
		$where = array();
		$condition = array();
		$condition['shop_title'] = $request->get('shop_title', '');
		if(0 < mb_strlen($condition['shop_title'], 'utf8')){
			$where['subscriber.title'] = array('like', '%%'.$condition['shop_title'].'%%');
		};
		$condition['shop_account'] = $request->get('shop_account', '');
		if(0 < mb_strlen($condition['shop_account'], 'utf8')){
			$where['subscriber.account'] = array('like', '%%'.$condition['shop_account'].'%%');
		};
		$view->assign('condition', $condition);
		
		$field = array(
			'`subscriber`.*',
		);
		$banner_select = \app\common\model\SubscriberModel::field($field)
			->where('group_id', $group_id)->where($where)
			->order('subscriber.id DESC')->paginate(10, false, array('query'=>$condition));
		$view->assign('page', $banner_select->render());
		
		$data_list = array();
		foreach($banner_select as $StaffModel){
			$data = $StaffModel->toArray();
			$data_list[] = $data;
		};
		$view->assign('data_list', $data_list);
		
		unset($where, $condition, $field, $banner_select, $StaffModel);
		return $view->fetch();
		
	}
	
}